package entities;

import java.io.Serializable;
import metadata.IndexMetaData;

/**
 *
 * @author Dragan Sahpaski
 */
public class Index implements Serializable {

    private Atribute atribute;
    // Tabelata na koj se gradi indexot - moze da i view
    private Table view;

    public Index(Table view, Atribute atribute) {
        this.atribute = atribute;
        atribute.setIndex(this);
        this.view = view;
    }

    public Atribute getAtribute() {
        return atribute;
    }

    public long size() {
        return atribute.getNumberOfDistinctValues();
    }

    public IndexMetaData getIndexMetaData(double diskPageSizeInBytes) {
        long distinctValues = atribute.getNumberOfDistinctValues();
        int logarithmBase = 10;
        long heightOfBPlusTree = (long) (Math.log(distinctValues) / Math.log(logarithmBase));
        int numberOfBitmapsPerLeave = 100;
        long numberOfLeaves = (long) (distinctValues / numberOfBitmapsPerLeave);
        if (numberOfLeaves == 0) {
            numberOfLeaves = 1;
        }
        if (heightOfBPlusTree == 0) {
            heightOfBPlusTree = 1;
        }
        double diskPagesToStoreABitmap = (double) view.getNumberOfTuples() / diskPageSizeInBytes;
        IndexMetaData imd = new IndexMetaData(distinctValues, numberOfLeaves, heightOfBPlusTree, (long) diskPagesToStoreABitmap);
        return imd;
    }
}
